package main

import "fmt"

func main() {
	var node1, node2, node3, node4, node5 *TreeNode
	//node5 = &TreeNode{7, nil, nil}
	//node4 = &TreeNode{15, nil, nil}
	node3 = &TreeNode{3, node4, node5}
	node2 = &TreeNode{1, nil, nil}
	node1 = &TreeNode{2, node2, node3}

	fmt.Println(isValidBST(node1))

}

type TreeNode struct {
	Val   int
	Left  *TreeNode
	Right *TreeNode
}

func isValidBST(root *TreeNode) bool {
	if root == nil {
		return true
	}
	var check func(*TreeNode) bool
	var rootVal = root.Val
	check = func(node *TreeNode) bool {
		if node == nil {
			return true
		}
		if node.Left != nil && (node.Left.Val >= node.Val || node.Left.Val >= rootVal) {
			return false
		}
		if node.Right != nil && (node.Right.Val <= node.Val || node.Right.Val <= rootVal) {
			return false
		}
		if !check(node.Left) || !check(node.Right) {
			return false
		}
		return true
	}
	return check(root)
}

func printTree(root *TreeNode) {
	if root == nil {
		fmt.Printf("%+v \t", nil)
		return
	}
	fmt.Printf("%d \t", root.Val)
	printTree(root.Left)
	printTree(root.Right)
}
